﻿function VIUIndexViewModel() {
    var self = this;
    self.dataList = ko.observableArray([]);
    self.statusText = ko.observable("");
    self.todo = "";
    self.goToReView = function (data) {
        window.app.loadUI('/VIU/Review/' + data.id);
    }
    self.deleteItem = function (data) {
        swal({
            title: "请确认是否删除？",
            text: "您正在进行删除操作，该操作不可逆!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger"
        }).then(function (isConfirm) {
            if (isConfirm === true) {
                app.ajax({
                    type: "DELETE",
                    url: 'api/CMSService/DeleteVIUs?ids=' + data.id,
                    data: {},
                    dataType: "json",
                    success: function (res) {
                        self.dataList.remove(data);
                        swal("已经删除", "你已经删除成功！", "success");
                    }
                });
            }
        });
    }
    self.getPage = function (pageNo) {
        self.statusText("正在加载数据！");
        var pageopt = {
            draw: pageNo,
            length: 15,
            start: (pageNo - 1) * 15
        };
        app.ajax({
            type: "POST",
            url: '/VIU/GetPaged?todo=' + self.todo,
            data: pageopt,
            dataType: "json",
            success: function (res) {
                res.data.forEach(function (item) {
                    //数据加工
                    item.starHtml = '';
                    var i = item.stars;
                    while (i > 0) {
                        item.starHtml = '<span class="star"><i class="glyphicon glyphicon-star"></i></span>' + item.starHtml;
                        i--;
                    }
                    i = 5 - item.stars;
                    while (i > 0) {
                        item.starHtml = item.starHtml + '<span class="star"><i class="glyphicon glyphicon-star-empty""></i></span>';
                        i--;
                    }
                    item.checkInTime = new Date(item.checkInTime).format("yyyy-MM-dd");

                    self.dataList.push(item);
                });

                if (self.dataList().length === 0)
                    self.statusText("无数据。");
                else {
                    self.statusText(self.dataList().length.toString());
                }
            }
        });
    }
    self.init = function () {
        self.getPage(1);
    }
}